# libs ----
library(geomander)
library(sf)
library(tigris)
sf::sf_use_s2(FALSE)


RC_vf_sf <- readRDS("data-raw/RC_vf_sf.Rds")
east_ramapo <- tigris::school_districts('NY')
east_ramapo <- east_ramapo %>% filter(str_detect(NAME, 'East Ramapo'))

ints <- st_intersects(RC_vf_sf, east_ramapo)
ints <- lapply(ints, function(x){if(length(x) == 0) {FALSE} else {TRUE}} )
ints <- unlist(ints)

RC_vf_sf <- RC_vf_sf %>% filter(ints)

blcks <- tigris::blocks('NY', year = 2010)

blcks <- blcks %>% filter(COUNTYFP10 == '087')
blcks <- blcks %>% geo_filter(east_ramapo) %>% geo_trim(east_ramapo)

er_geoids <- blcks$GEOID10
saveRDS(er_geoids, 'data/ERCSD/er_geoids.Rds')

mtches <- geo_match(from = RC_vf_sf, to = blcks)

RC_vf_sf$GEOID10 <- blcks$GEOID10[mtches]

saveRDS(RC_vf_sf, 'data-raw/east_ramapo_voterfile.Rds')
